package androidgames.data;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.os.Environment;
import android.util.Log;

 
public class CheckData {

	public CheckData() {

	}

 
	public boolean checkFileMissing(String filePath) {
		java.io.File file = new java.io.File(filePath);
		if (file.exists()) {
			return true;
		}
		return false;
	}

	public void downloadFile(String URL, String storePath) {
		try {
			URL url = new URL(URL);
			HttpURLConnection c = (HttpURLConnection) url.openConnection();
			c.setRequestMethod("GET");
			c.setDoOutput(true);
			c.connect();

			String PATH = Environment.getExternalStorageDirectory() + storePath;
			Log.v("Path: ", "PATH: " + PATH);
			File file = new File(PATH);
			file.mkdirs();

			String fileName = URL.toString().substring(
					URL.toString().lastIndexOf('/'));

			File outputFile = new File(file, fileName);
			FileOutputStream fos = new FileOutputStream(outputFile);

			InputStream is = c.getInputStream();

			byte[] buffer = new byte[1024];
			int len1 = 0;
			while ((len1 = is.read(buffer)) != -1) {
				fos.write(buffer, 0, len1);
			}
			fos.close();
			is.close();

			// }
		} catch (IOException e) {
			Log.d("Exception: ", "Error: " + e);
			// Toast.makeText(myApp, "error " + e.toString(), Toast.LENGTH_LONG)
			// .show();

		}

	}

}
